package com.wejoy.video.middle.station.data.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ip工具类
 */
public class IPUtil {

    private static final String IP_UTILS_FLAG = ",";

    /**
     * 获得用户远程地址
     */
    public static String getRemoteAddress(HttpHeaders headers) {
        List<String> remoteAddrList = headers.get("X-Real-IP");
        if (CollectionUtils.isEmpty(remoteAddrList)) {
            remoteAddrList = headers.get("X-Forwarded-For");
        } else if (CollectionUtils.isEmpty(remoteAddrList)) {
            remoteAddrList = headers.get("Proxy-Client-IP");
        } else if (CollectionUtils.isEmpty(remoteAddrList)) {
            remoteAddrList = headers.get("WL-Proxy-Client-IP");
        }
        if (CollectionUtils.isEmpty(remoteAddrList)) {
            return "";
        }
        String remoteAddr = remoteAddrList.get(0);
        //使用代理，则获取第一个IP地址
        if (StringUtils.isNotBlank(remoteAddr) && remoteAddr.indexOf(IP_UTILS_FLAG) > 0) {
            remoteAddr = remoteAddr.substring(0, remoteAddr.indexOf(IP_UTILS_FLAG));
        }
        return remoteAddr;
    }

    public static String getRemoteAddress(Map<String, String> map) {
        String remoteAddr = map.get("X-Real-IP");
        if (StringUtils.isEmpty(remoteAddr)) {
            remoteAddr = map.get("X-Forwarded-For");
        } else if (StringUtils.isEmpty(remoteAddr)) {
            remoteAddr = map.get("Proxy-Client-IP");
        } else if (StringUtils.isEmpty(remoteAddr)) {
            remoteAddr = map.get("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(remoteAddr)) {
            return "";
        }
        //使用代理，则获取第一个IP地址
        if (StringUtils.isNotBlank(remoteAddr) && remoteAddr.indexOf(IP_UTILS_FLAG) > 0) {
            remoteAddr = remoteAddr.substring(0, remoteAddr.indexOf(IP_UTILS_FLAG));
        }
        return remoteAddr;
    }

    public static String getRemoteAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(IP_UTILS_FLAG) != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("remoteip");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteHost();
        }

        return ip.replaceAll(":", ".");
    }


    /**
     * aj captcha使用的
     *
     * @param request
     * @return
     */
    public static final String getRemoteId(HttpServletRequest request) {
        String xfwd = IPUtil.getRemoteAddress(request);
        String ip = getRemoteIpFromXfwd(xfwd);
        String ua = request.getHeader("user-agent");
        if (com.anji.captcha.util.StringUtils.isNotBlank(ip)) {
            return ip + ua;
        }
        return request.getRemoteAddr() + ua;
    }

    private static String getRemoteIpFromXfwd(String xfwd) {
        if (com.anji.captcha.util.StringUtils.isNotBlank(xfwd)) {
            String[] ipList = xfwd.split(",");
            return com.anji.captcha.util.StringUtils.trim(ipList[0]);
        }
        return null;
    }

}
